package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.Scheme;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.AddKehuActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.mine.shenqing.CreateNewsActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;

public class AddSaleChanceActivity extends SlimBaseMultiEditActivity {
    private AddKehuActivityCustom dataBinding;
    private String name;
    private String departmentName;
    private String careerName;
    private String phone;
    private String qq;
    private String weixin;
    private String email;
    private String msg;
    private int type;
    String cusid;
    @ToSave
    String contacts;

    private ActionSheetDialog chooseDialogRemind;
    private String clientName;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_kehu);
        initTopTitleBar(View.VISIBLE, "添加销售机会");
        initTopRightText("添加", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.KEHU_ADD_SALE_CHANGE)
                        .params("owner", user.getGuid())
                        .params("comid", user.getCompanyId())
                        .params("chanceName", setParam(0))
                        .params("chanceClassify", setParam(1))
                        .params("cusid", cusid)
                        .params("contacts", contacts)
                        .params("interestproducts", setParam(4))
                        .params("exdateofcompletion", setParam(5))
                        .params("productsalesmoney", setParam(6))
                        .params("expectmoney", setParam(7))
                        .params("remark", setParam(8))
                        .params("remindTime", setParam(9))
                        .execute(new DialogCallBack<HttpResult>(AddSaleChanceActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave=false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
        type = getIntent().getIntExtra(Config.TYPE, -1);
        if (type != -1) {
            isRead = false;
        }
        name = getIntent().getStringExtra(Config.USER_NAME);
        cusid = getIntent().getStringExtra("cusid");
        departmentName = getIntent().getStringExtra(Config.DEPARTMENT_NAME);
        careerName = getIntent().getStringExtra(Config.CAREER_NAME);
        clientName = getIntent().getStringExtra(Config.CLIENT_NAME);
        phone = getIntent().getStringExtra(Config.PHONE);
        qq = getIntent().getStringExtra(Config.QQ);
        weixin = getIntent().getStringExtra(Config.WEIXIN);
        email = getIntent().getStringExtra(Config.EMAIL);
    }


    private boolean checkParam() {
        if (TextUtils.isEmpty(datas.get(0).getContent())) {
            msg = "请机会名称";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(1).getContent())) {
            msg = "请选择机会分类";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(3).getContent())) {
            msg = "请选择联系人";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(4).getContent())) {
            msg = "请输入兴趣产品";
            return false;
        }

        return true;
    }

    @Override
    protected void recoverAfter() {
        datas.get(2).setContent(clientName);
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (slimAdapterEx.getData().get(p).getPosition()) {
            case 3:
                Bundle bundle = new Bundle();
                bundle.putString("cusid", cusid);
                startActivityForResult(ChooseLianxirenActivity.class, bundle, Config.CHOOSE_LIANXIREN);
                break;
            case 1:
                if (chooseDialogRemind == null) {
                    SingleChooseUtil.getChooseDialog(AddSaleChanceActivity.this, 37, (sheet) -> {
                        chooseDialogRemind = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    chooseDialogRemind.show();
                }
                break;

        }
    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("*机会名称", name, 0, "请输入(必填)"));
        datas.add(new SlimEditEnterBean("*机会分类", departmentName, 1, true, "请选择(必选)"));
        datas.add(new SlimEditHintBean("客户", clientName, 2));
        datas.add(new SlimEditEnterBean("*联系人", phone, 3, true, "请选择(必选)"));
        datas.add(new SlimEditEnterBean("*兴趣产品", qq, 4, "请输入(必填)"));
        datas.add(new SlimEditTimeBean("预期成交日期", weixin, 5, "请输入"));
        datas.add(new SlimEditEnterBean("预期金额（元）", email, 6, "请输入").addSchemes(StaticScheme.Numeric()));
        datas.add(new SlimEditEnterBean("产品销售金额（元）", email, 7, "请输入").addSchemes(StaticScheme.Numeric()));
        datas.add(new SlimEditEnterBean("备注", "", 8, "请填写"));
        datas.add(new SlimEditTimeBean("提醒", "", 9, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.CHOOSE_LIANXIREN:
                    LianxirenListResultBean bean = (LianxirenListResultBean) data.getSerializableExtra("bean");
                    contacts = bean.getId() + "";
                    slimAdapterEx.getData().get(3).setContent(bean.getName());
                    slimAdapterEx.notifyItemChanged(3);
                    break;
            }
        }
    }
}
